import config from 'config'
import httpFetch from 'share/httpFetch'
import errorMessage from 'share/errorMessage'

export default {

  //获取审批流列表，booksID：帐套id
  getWorkflowList(booksID) {
    let params = {};
    booksID && (params.booksID = booksID);
    return httpFetch.get(`${config.baseUrl}/brms/api/rule/custom/forms`, params)
  },

  //复制审批链
  copyApproveChains(sourceFormOID, targetFormOID) {
    let params = {
      sourceFormOID,
      targetFormOID
    };
    return new Promise((resolve, reject) => {
      httpFetch.put(`${config.baseUrl}/brms/api/rule/approval/chains/copy/v2`, params).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e)
      })
    })
  },

  //获取审批链详情
  getApprovalChainDetail(formOID, setOfBooksId, version) {
    let params = {
      formOID,
      setOfBooksId,
      version: version,
      cascadeApprovalNode: true,
      cascadeApprover: true,
      cascadeCondition: true
    };
    return httpFetch.get(`${config.baseUrl}/brms/api/rule/approval/chains/form`, params)
  },

  //删除节点
  deleteApprovalNode(ruleApprovalNodeOID) {
    return new Promise((resolve, reject) => {
      httpFetch.delete(`${config.baseUrl}/brms/api/rule/approval/nodes/${ruleApprovalNodeOID}`).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e)
      })
    })
  },

  //获取表单配置
  getCustomFormProperty(formOID) {
    return httpFetch.get(`${config.baseUrl}/api/customForm/property/${formOID}`)
  },

  //保存表单配置
  saveCustomFormProperty(params) {
    return new Promise((resolve, reject) => {
      httpFetch.put(`${config.baseUrl}/api/customForm/property`, params).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e)
      })
    })
  },

  //创建审批节点
  createApprovalNodes(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(`${config.baseUrl}/brms/api/rule/approval/nodes`, params).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e)
      })
    })
  },

  //修改审批节点
  modifyApprovalNodes(params) {
    return new Promise((resolve, reject) => {
      httpFetch.put(`${config.baseUrl}/brms/api/rule/approval/nodes`, params).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e)
      })
    })
  },

  //移动审批节点
  moveApprovalNode(ruleApprovalNodeOID, nextRuleApprovalNodeOID) {
    return new Promise((resolve, reject) => {
      httpFetch.put(`${config.baseUrl}/brms/api/rule/approval/nodes/move?ruleApprovalNodeOID=${ruleApprovalNodeOID}&nextRuleApprovalNodeOID=${nextRuleApprovalNodeOID || ''}`).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e)
      })
    })
  },

  //获取审批者类型
  getApproverType() {
    return httpFetch.get(`${config.baseUrl}/brms/api/rule/approver/types`);
  },

  //批量获取部门
  getDepartmentSimpleList(params) {
    return httpFetch.post(`${config.baseUrl}/api/departments/oids/simple`, params)
  },

  //批量获取成本中心
  getBatchCostCenterList(params) {
    return httpFetch.post(`${config.baseUrl}/api/cost/center/items/oids`, params)
  },

  //批量获取公司控件
  getBatchCompanyItemList(params) {
    let url = `${config.baseUrl}/api/company/by/companyOIDs`;
    params && params.map((oid, index) => {
      url += index === 0 ? `?companyOIDs=${oid}` : `&companyOIDs=${oid}`
    });
    return httpFetch.get(url)
  },

  //批量获取法人实体
  getBatchCorporationEntityList(params) {
    return httpFetch.post(`${config.baseUrl}/api/company/receipted/invoice/oids`, params)
  },

  //批量获取值列表
  getCustomEnumerationList(params) {
    return httpFetch.post(`${config.baseUrl}/api/custom/enumeration/items/oids`, params)
  },

  //批量获取人员
  getBatchUserList(params) {
    return httpFetch.post(`${config.baseUrl}/api/users/oids`, params)
  },

  //表单字段获取(审批条件)
  getFormFields( formOID, version ) {
    let params = {
      formOID,
      version
    };
    return httpFetch.get(`${config.baseUrl}/brms/api/rule/custom/form/fields`, params)
  },

  //创建审批者
  createApprovers(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(`${config.baseUrl}/brms/api/rule/approvers/batch`, params).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e)
      })
    })
  },

  //修改审批者
  updateApprovers: function(params) {
    return new Promise((resolve, reject) => {
      httpFetch.put(`${config.baseUrl}/brms/api/rule/approvers`, params).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e)
      })
    })
  },

  //移除审批者
  deleteApprovers(ruleApproverOID) {
    return new Promise((resolve, reject) => {
      httpFetch.delete(`${config.baseUrl}/brms/api/rule/approvers/${ruleApproverOID}`).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e)
      })
    })
  },

  //所有的条件操作符
  getSymbolsType() {
    return httpFetch.get(`${config.baseUrl}/brms/api/rule/approval/symbols`)
  },

  //批量创建审批者条件
  createRuleCondition(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(`${config.baseUrl}/brms/api/rule/conditions/batch`, params).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e)
      })
    })
  },

  //批量修改审批者条件
  updateRuleCondition(params) {
    return new Promise((resolve, reject) => {
      httpFetch.put(`${config.baseUrl}/brms/api/rule/conditions/batch`, params).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e)
      })
    })
  },

  //批量删除审批者条件, params ruleConditionOID数组
  deleteRuleCondition(params) {
    return new Promise((resolve, reject) => {
      httpFetch.delete(`${config.baseUrl}/brms/api/rule/conditions/batch`, params).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e)
      })
    })
  },

  //获取组织架构列表
  getDepartmentPositionList() {
    return httpFetch.get(`${config.baseUrl}/api/departmentposition`)
  },

  //获取自钉子列表的值列表
  getBatchTypeList(params) {
    return httpFetch.get(`${config.baseUrl}/api/custom/enumeration/system/by/type`, params)
  },
  getBatchUsers(params) {

    let url = `${config.baseUrl}/api/users/oids`;
    params && params.map((oid, index) => {
      url += index === 0 ? `?userOIDs=${oid}` : `&userOIDs=${oid}`
    });
    return httpFetch.get(url);
  },

  //获取扩展字段表单
  getCustomForm(oid) {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/custom/forms/' + oid + '/simple')
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  //根据值列表OID获取值列表信息
  getValueListInfo(OID) {
    return new Promise(function(resolve, reject) {
      httpFetch.get(config.baseUrl + '/api/custom/enumerations/' + OID)
        .then(function(res) {
          resolve(res)
        })
        .catch(function(err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  //根据值列表OID获取值列表信息
  getLoanTypeList(params) {
    return new Promise(function(resolve, reject) {
      httpFetch.get(config.payUrl + '/api/cash/transaction/classes/queryBySetOfBookId/' + params.setOfBookId)
        .then(function(res) {
          resolve(res)
        })
        .catch(function(err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  //获取费用包含标签
  getInvoiceLabels(labelCategory){
    //1 单据标签 2 费用标签
    let param = {
      labelCategory
    };
    return httpFetch.get(`${config.baseUrl}/api/expense/labels/approval`, param)
  },

  getDepartmentGroupList(param){
    return httpFetch.post(`${config.baseUrl}/api/DepartmentGroup/get/group/by/codes`,param)
  },

  checkRemarkType(remark){
    let dataType = {
      long: ['out_participant_num', 'custom_form_travel_day'],//整数类型
      double: ['total_budget', 'average_budget', 'default_total_amount', 'amount', 'default_amount', 'number', 'default_credit_score'],//浮点类型
      date: ['start_date', 'end_date', 'date', 'common.date'],//日期类型
      text: ['title', 'input', 'remark', 'out_participant_name', 'text_area', 'select_box',
        'default_user_work_number', 'default_user_position'],//文本类型
      boolean: ['boolean', 'writeoff_flag', 'substitution_invoice', 'control_beyound_application',
        'control_beyound_position', 'judge_cost_center', 'control_beyound_budget', 'control_beyound_travel_standard',
        'switch', 'linkage_switch', 'default_travel_application_version'],//布尔类型
      custList: ['select_participant', 'select_approver', 'applicant', 'select_user', 'select_cost_center',
        'select_department', 'select_special_booking_person', 'select_corporation_entity',
        'default_corporation_entity', 'default_expense_type', 'default_department', 'currency_code',
        'select_air_ticket_supplier', 'default_department_level', 'default_department_path', 'custom_form_department_path'
        , 'default_department_role', 'select_company', 'default_applicant_company', 'default_user_applicant'
        , 'default_user_department', 'default_user_direct_leadership', 'default_loan_type', 'control_beyound_label',
        'custom_form_department_role', 'default_department_group', 'custom_form_department_group'],//chooser类型
      selector: ['default_user_sex', 'default_user_level', 'default_user_post', 'default_user_category',
        'cust_list', 'default_user_department_extend', 'custom_form_department_extend', 'default_credit_level',
        'custom_form_department_level', 'control_beyound_expense_label'],//值列表
      combo: ['default_expense_type_amount', 'default_each_one_expense_type_amount'],//组合类型
    };
    return Object.keys(dataType).map(item => {
      return dataType[item].includes(remark) && item;
    }).filter(obj => obj)[0];
  },

  //default_department_level改为selector类型
  /**
   * custList 需要用chooser控件
   * selector 多选值列表
   * combo 特殊组合控件
   * **/

  checkNewRemarkType(remark){
    let dataType = {
      long: ['out_participant_num', 'custom_form_travel_day'],//整数类型
      double: ['total_budget', 'average_budget', 'default_total_amount', 'amount', 'default_amount', 'number', 'default_credit_score'],//浮点类型
      date: ['start_date', 'end_date', 'date', 'common.date'],//日期类型
      text: ['title', 'input', 'remark', 'out_participant_name', 'text_area', 'select_box',
        'default_user_work_number', 'default_user_position'],//文本类型
      boolean: ['boolean', 'writeoff_flag', 'substitution_invoice', 'control_beyound_application',
        'control_beyound_position', 'judge_cost_center', 'control_beyound_budget', 'control_beyound_travel_standard',
        'switch', 'linkage_switch', 'default_travel_application_version'],//布尔类型
      custList: ['select_participant', 'select_approver', 'applicant', 'select_user', 'select_cost_center',
        'select_department', 'select_special_booking_person', 'select_corporation_entity',
        'default_corporation_entity', 'default_expense_type', 'default_department', 'currency_code',
        'select_air_ticket_supplier', 'default_department_path', 'custom_form_department_path'
        , 'default_department_role', 'select_company', 'default_applicant_company', 'default_user_applicant'
        , 'default_user_department', 'default_user_direct_leadership', 'default_loan_type', 'default_department_group',
        'custom_form_department_role', 'default_department_group', 'custom_form_department_group', 'default_user_group'],//chooser类型
      selector: ['default_user_sex', 'default_department_level', 'default_user_level', 'default_user_post', 'default_user_category',
        'cust_list', 'default_user_department_extend', 'custom_form_department_extend', 'default_credit_level', 'control_beyound_label'
        , 'custom_form_department_level', 'control_beyound_expense_label', 'default_auto_audit_result', 'default_stroke_type'],//值列表
      combo: ['default_expense_type_amount', 'default_each_one_expense_type_amount', 'default_average_amount'],//组合类型
    };
    return Object.keys(dataType).map(item => {
      return dataType[item].includes(remark) && item;
    }).filter(obj => obj)[0];
  }
}
